/* ----------------------------------------------------------------------------
 * Copyright (c) Huawei Technologies Co., Ltd. 2013-2019. All rights reserved.
 * Description: ShellCmd SysInfo
 * Author: liulei
 * Create: 2013-01-01
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * --------------------------------------------------------------------------- */
/* ----------------------------------------------------------------------------
 * Notice of Export Control Law
 * ===============================================
 * Huawei LiteOS may be subject to applicable export control laws and regulations, which might
 * include those applicable to Huawei LiteOS of U.S. and the country in which you are located.
 * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such
 * applicable export control laws and regulations.
 * --------------------------------------------------------------------------- */
#include "los_config.h"
#ifdef LOSCFG_SHELL
#include "los_sem.h"
#include "los_mux.h"
#include "los_queue.h"
#include "los_swtmr.h"
#include "string.h"
#include "unistd.h"
#include "los_sem_pri.h"
#include "los_mux_pri.h"
#include "los_queue_pri.h"
#include "los_swtmr_pri.h"

#include "shcmd.h"
#include "shell.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* __cplusplus */

#define SYSINFO_ENABLED(x) (((x) == YES) ? "YES" : "NO")

UINT32 osShellCmdTaskCntGet(VOID)
{
    UINT32 loop;
    UINT32 taskCnt = 0;
    UINTPTR intSave;
    LosTaskCB *taskCB = (LosTaskCB *)NULL;

    intSave = LOS_IntLock();
    for (loop = 0; loop < g_taskMaxNum; loop++) {
        taskCB = (((LosTaskCB *)g_taskCBArray) + loop);
        if (taskCB->taskStatus & OS_TASK_STATUS_UNUSED) {
            continue;
        }
        taskCnt++;
    }
    (VOID)LOS_IntRestore(intSave);
    return taskCnt;
}

UINT32 osShellCmdSemCntGet(VOID)
{
    UINT32 loop;
    UINT32 semCnt = 0;
    UINTPTR intSave;
    LosSemCB *semNode = (LosSemCB *)NULL;

    intSave = LOS_IntLock();
    for (loop = 0; loop < LOSCFG_BASE_IPC_SEM_LIMIT; loop++) {
        semNode = GET_SEM(loop);
        if (semNode->semStat == OS_SEM_USED) {
            semCnt++;
        }
    }
    (VOID)LOS_IntRestore(intSave);
    return semCnt;
}

UINT32 osShellCmdMuxCntGet(VOID)
{
    UINT32 loop;
    UINT32 muxCnt = 0;
    UINTPTR intSave;
    LosMuxCB *muxNode = (LosMuxCB *)NULL;

    intSave = LOS_IntLock();
    for (loop = 0; loop < LOSCFG_BASE_IPC_MUX_LIMIT; loop++) {
        muxNode = GET_MUX(loop);
        if (muxNode->muxStat == OS_MUX_USED) {
            muxCnt++;
        }
    }
    (VOID)LOS_IntRestore(intSave);
    return muxCnt;
}

UINT32 osShellCmdQueueCntGet(VOID)
{
    UINT32 loop;
    UINT32 queueCnt = 0;
    UINTPTR intSave;
    LosQueueCB *queueCB = (LosQueueCB *)NULL;

    intSave = LOS_IntLock();
    queueCB = g_allQueue;
    for (loop = 0; loop < LOSCFG_BASE_IPC_QUEUE_LIMIT; loop++, queueCB++) {
        if (queueCB->queueState == OS_QUEUE_INUSED) {
            queueCnt++;
        }
    }
    (VOID)LOS_IntRestore(intSave);
    return queueCnt;
}

UINT32 osShellCmdSwtmrCntGet(VOID)
{
    UINT32 loop;
    UINT32 swTmrCnt = 0;
    UINTPTR intSave;
    SWTMR_CTRL_S *swTmrCB = (SWTMR_CTRL_S *)NULL;

    intSave = LOS_IntLock();
    swTmrCB = g_swtmrCBArray;
    for (loop = 0; loop < LOSCFG_BASE_CORE_SWTMR_LIMIT; loop++, swTmrCB++) {
        if (swTmrCB->ucState != OS_SWTMR_STATUS_UNUSED) {
            swTmrCnt++;
        }
    }
    (VOID)LOS_IntRestore(intSave);
    return swTmrCnt;
}

LITE_OS_SEC_TEXT_MINOR VOID osShellCmdSystemInfoGet(VOID)
{
    PRINTK("\n   Module    Used      Total     Enabled\n");
    PRINTK("--------------------------------------------\n");
    PRINTK("   Task      %-10d%-10d%s\n",
           osShellCmdTaskCntGet(),
           LOSCFG_BASE_CORE_TSK_LIMIT,
           SYSINFO_ENABLED(YES)); /*lint !e506*/
    PRINTK("   Sem       %-10d%-10d%s\n",
           osShellCmdSemCntGet(),
           LOSCFG_BASE_IPC_SEM_LIMIT,
           SYSINFO_ENABLED(LOSCFG_BASE_IPC_SEM)); /*lint !e506*/
    PRINTK("   Mutex     %-10d%-10d%s\n",
           osShellCmdMuxCntGet(),
           LOSCFG_BASE_IPC_MUX_LIMIT,
           SYSINFO_ENABLED(LOSCFG_BASE_IPC_MUX)); /*lint !e506*/
    PRINTK("   Queue     %-10d%-10d%s\n",
           osShellCmdQueueCntGet(),
           LOSCFG_BASE_IPC_QUEUE_LIMIT,
           SYSINFO_ENABLED(LOSCFG_BASE_IPC_QUEUE)); /*lint !e506*/
    PRINTK("   SwTmr     %-10d%-10d%s\n",
           osShellCmdSwtmrCntGet(),
           LOSCFG_BASE_CORE_SWTMR_LIMIT,
           SYSINFO_ENABLED(LOSCFG_BASE_CORE_SWTMR)); /*lint !e506*/
}

INT32 osShellCmdSystemInfo(INT32 argc, CHAR **argv)
{
    if (argc == 0) {
        osShellCmdSystemInfoGet();
        return 0;
    } else {
        PRINTK("systeminfo: invalid option %s\n"
               "Systeminfo has NO ARGS.\n",
               argv[0]);
        return -1;
    }
}

SHELLCMD_ENTRY(systeminfo_shellcmd, CMD_TYPE_EX, "systeminfo", 1, (CMD_CBK_FUNC)osShellCmdSystemInfo); /*lint !e19*/
#endif

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */